package com.veryedu.hibernate.hbm.dao.impl;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.veryedu.hibernate.hbm.dao.ShowTimeDao;
import com.veryedu.hibernate.hbm.entity.Changci;

public class ShowTimeDaoImpl extends BaseDaoImpl implements ShowTimeDao{

	@Override
	public List<Changci> findBetweenTime(Timestamp startTime, Timestamp endTime) {
		List<Changci> list=null;
		getSession();
		this.tx=session.beginTransaction();
		try {
			Criteria cri=session.createCriteria(Changci.class);
			Criterion cron=Restrictions.between("playtime", startTime, endTime);
			Criterion cron1=Restrictions.lt("price", new Float(80.0));
			cri.add(cron);
			cri.add(cron1);
			list=cri.list();
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			closeSession();
		}
		return list;
	}

	@Override
	public List<Changci> findByRoomName(String roomname) {
		List<Changci> list=null;
		getSession();
		this.tx=session.beginTransaction();
		try {
			//QBC
			/*Criteria cri=session.createCriteria(Changci.class);
			Criterion cron=Restrictions.between("playtime", startTime, endTime);
			Criterion cron1=Restrictions.lt("price", new Float(80.0));
			cri.add(cron);
			cri.add(cron1);
			list=cri.list();*/
			
			/*Criteria cri=session.createCriteria(Changci.class);
			cri.createAlias("room", "r");
			Criterion cron=Restrictions.eq("r.roomname", roomname);
			cri.add(cron);*/
			
			//HQL
			String hql="from Changci as c where c.room as r r.roomname=:?";
			Query query=session.createQuery(hql);
			query.setString(1, roomname);
			list=query.list();
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			closeSession();
		}
		return list;
	}
	
}
